import unittest

from django.core.exceptions                 import ValidationError
from sitepal.sitebase.models                import Property
from sitepal.sitebase.tests.models.entities import EntityTest

class PropertyTest(EntityTest):
    def create_object(self,obj,save=True,**kwargs):
        self.assertTrue(isinstance(obj,Property))
        obj.name            = kwargs['name']
        obj.value           = kwargs['value']
        obj.validator_names = kwargs.get('validator_names')
        obj.to_python_name  = kwargs.get('to_python_name')
        obj                 = EntityTest.create_object(self,obj,save=save,**kwargs)
        return obj

    def verify_object(self,obj,**kwargs):
        self.assertTrue(isinstance(obj,Property))
        EntityTest.verify_object(self,obj,**kwargs)
        self.assertTrue(obj.name  == kwargs['name'])
        self.assertTrue(obj.value == kwargs['value'])
        self.assertTrue(obj.get_value() == obj.value)

    def create_property(self,**kwargs):
        obj = self.create_object(Property(),save=True,**kwargs)
        self.verify_object(obj,**kwargs)
        return obj

    def testCreate(self):
        p1 = self.create_property(name ='site-content-type',
                                  value='text/html; charset=utf-8')
        p2 = self.create_property(name ='site-sidebar-bg',
                                  value='#DDFFA6')
        p3 = self.create_property(name ='site-footer-bg',
                                  value='#FF8ACD')
        p4 = self.create_property(name ='user-login-redirect-to',
                                  value='/user/profile/')
        # Duplicate name
        self.assertRaises(ValidationError,self.create_property,
                          name ='user-login-redirect-to',value='/user/profile/',usage='admin')

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(PropertyTest)
